//
//  LGHomeViewController+subViews.m
//  LuxuryGoods
//
//  Created by 纳里健康 on 2018/5/23.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGHomeViewController+subViews.h"
#import "BrandCollectionViewCell.h"
#import "LGStyleView.h"
#import "LGHomeSearcjViewController.h"
#import "LGDetailCollectionViewCell.h"

#import "LGBestSellerViewController.h"
#import "LGRedPacketViewController.h"
#import "LGConserveViewController.h"
#import "LGAuthenticateViewController.h"
#import "LGHomeSearcjViewController.h"
#import "LGClassifyViewController.h"
#import "LGProductDetailViewController.h"

#import "LGBannerModel.h"
#import "LGBrandModel.h"
#import "LGActivityModel.h"
#import "LGPalaceModel.h"
#import "LGItemModel.h"

static CGFloat brandScrollWidth;

@implementation LGHomeViewController (subViews)

#pragma mark -- 轮播图
- (SDCycleScrollView *)makeCircleImageView
{
    SDCycleScrollView *circleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (SCREEN_WIDTH / 72) * 35) delegate:self placeholderImage:nil];
    //设置pageController
    circleView.currentPageDotImage = [UIImage imageNamed:@"currentPage"];
    circleView.pageDotImage = [UIImage imageNamed:@"otherPage"];
    circleView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    
    //设置数据源
    NSArray *bannerArray = self.dataModel.banner;
    
    NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:bannerArray.count];
    
    for (LGBannerModel *bannerModel in bannerArray) {
      
        NSString *urlString = bannerModel.ad_img;
        
        [imageArray addObject:urlString];
    };
    
    circleView.imageURLStringsGroup = imageArray;
    
    circleView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    
    return circleView;
}

#pragma mark -- 轮播点击代理
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSArray *bannerArray = self.dataModel.banner;
    
    LGBannerModel *bannerModel = bannerArray[index];
    
    NSInteger typeValue = [bannerModel.jump_type integerValue];
    
    //jump_type -> banner跳转方式 0-不跳转 1-单品详情页 2-活动会场 3-url
    switch (typeValue) {
        case 0:
            break;
        case 1:
        {
            //单品详情页
            NSString *productId = bannerModel.to_id;
        }
            break;
        case 2:
            {
                //活动会场
                NSString *activityID = bannerModel.to_id;
            }
            break;
        case 3:
            {
                //URl
                NSString *utlString = bannerModel.ad_link;
            }
            break;
            
        default:
            break;
    }
    
    
}

#pragma mark -- 顶部搜索栏
- (UIButton *)makeSearBar
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = RGBACOLOR(238, 238, 238, 0.3);
    [btn  setTitle:@"探索你的奢物" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"sousuo"] forState:UIControlStateNormal];
    btn.layer.cornerRadius = 15;
    btn.layer.masksToBounds = YES;
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
    [btn addTarget:self action:@selector(goToSearch) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

#pragma mark -- 去搜索界面
- (void)goToSearch
{
    LGHomeSearcjViewController *searchView = [[LGHomeSearcjViewController alloc] init];
    
    [self.navigationController pushViewController:searchView animated:YES];
    
}


#pragma mark -- 5个功能View
- (UIView *)makeFiveBtnViewWithFrame:(CGRect)frame
{
    UIView *fiveView = [[UIView alloc] initWithFrame:frame];
    fiveView.backgroundColor = WHITE_COLOR;
    NSInteger count = 5;
    CGFloat itemWidt = 50;
    CGFloat itemHeight = 60;
    CGFloat space = (SCREEN_WIDTH - count * itemWidt)/(count + 1);
    
    NSArray *imageArray = @[@"shouye_hongbao",@"shouye_jianding",@"shouye_shangxin",@"shouye_yanghu",@"shouye_fenqi"];
    NSArray *titleArray = @[@"领红包",@"鉴定",@"上新",@"养护",@"分期"];
    
    for (int i = 0 ; i < 5; i ++) {
        
        CGFloat itemX = i * itemWidt + (i + 1) * space;

        UIView *itemView = [[UIView alloc] initWithFrame:CGRectMake(itemX, (frame.size.height - itemHeight)/2, itemWidt, itemHeight)];
        
        itemView.tag = 100 + i;
        
        [fiveView addSubview:itemView];
        
        UIImageView *tagImage = [[UIImageView alloc] initWithFrame:CGRectMake((itemWidt - 23)/2, 10, 19, 23)];
        tagImage.image = [UIImage imageNamed:imageArray[i]];
        [itemView addSubview:tagImage];
        
        UILabel *tagLael = [[UILabel alloc] initWithFrame:CGRectMake(0, 44, itemWidt, FONT(13).lineHeight)];
        tagLael.font = [UIFont systemFontOfSize:13];
        tagLael.textAlignment = NSTextAlignmentCenter;
        [itemView addSubview:tagLael];
        tagLael.text = titleArray[i];
        
        
        UITapGestureRecognizer *singelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemClick:)];
        
        [itemView addGestureRecognizer:singelTap];
    }

    return fiveView;
}

- (void)itemClick:(id)recognize
{
    UITapGestureRecognizer *tapClick = (UITapGestureRecognizer *)recognize;
    
    UIView *membwerView = tapClick.view;
    
    NSInteger tag = membwerView.tag - 100;
    
    NSLog(@"click At Tag == %ld",(long)tag);
    
    switch (tag) {
        case 0:
        {
            //领红包
            LGRedPacketViewController *redPacket = [[LGRedPacketViewController alloc] initWithNibName:@"LGRedPacketViewController" bundle:nil];
            
            [self.navigationController pushViewController:redPacket animated:YES];
        }
            break;
        case 1:
        {
            //鉴定
            LGAuthenticateViewController *anthenticatVC = [[LGAuthenticateViewController alloc] init];
            [self.navigationController pushViewController:anthenticatVC animated:YES];
        }
            break;
        case 2:
        {
            //上新
            LGHomeSearcjViewController *searchVC = [LGHomeSearcjViewController new];
            [self.navigationController pushViewController:searchVC animated:YES];
        }

            break;
        case 3:
        {
            //养护
            LGConserveViewController *conserveView = [[LGConserveViewController alloc] init];
            
            [self.navigationController pushViewController:conserveView animated:YES];
        }

            break;
        case 4:
        {
            //分期
            LGHomeSearcjViewController *searchVC = [LGHomeSearcjViewController new];
            [self.navigationController pushViewController:searchVC animated:YES];
        }
            break;

        default:
            break;
    }
    
}


#pragma mark 品牌的View
- (UIView *)makeBrandViewWithFrame:(CGRect)frame
{
    UIView *brandView = [[UIView alloc] initWithFrame:frame];
    brandView.backgroundColor = WHITE_COLOR;
    UIImageView *nextImgeView = [[UIImageView alloc] initWithFrame: CGRectMake(SCREEN_WIDTH - 35, (frame.size.height - 25)/2, 25, 25)];
    nextImgeView.image = [UIImage imageNamed:@"rightRow"];
    [brandView addSubview:nextImgeView];
    UITapGestureRecognizer *nextTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNextBran:)];
    nextImgeView.userInteractionEnabled = YES;
    [nextImgeView addGestureRecognizer:nextTap];
    
    CGFloat nextBtnLeft = 35;
    //设置总个数
    NSInteger brandCount = self.dataModel.brand.count;
    
    CGFloat itemWidth = ceilf(frame.size.width - (nextBtnLeft + 5) - 5 * 5)/4;
    
    CGFloat itemHeight = itemWidth + 10;
    
    UIScrollView *brandScrollView = [[UIScrollView alloc] init];
    //获取每个Item的宽度
    CGFloat scrollWidth = frame.size.width - (nextBtnLeft + 5);
    brandScrollWidth = scrollWidth;
    brandScrollView.frame = CGRectMake(0, 0, scrollWidth, frame.size.height);
    
    brandScrollView.contentSize = CGSizeMake(itemWidth * 15 + 5 * (brandCount + 1), frame.size.height);
    brandScrollView.tag = 421;
    brandScrollView.scrollEnabled = YES;
    brandScrollView.showsHorizontalScrollIndicator = NO;
    
    [brandView addSubview:brandScrollView];
    
    for (int i = 0 ; i < brandCount; i ++) {
        BrandCollectionViewCell *btnView = [[NSBundle mainBundle] loadNibNamed:@"BrandCollectionViewCell" owner:self options:nil][0];
        
        CGFloat itemX = i * itemWidth + (i + 1) * 5;
        
        btnView.frame = CGRectMake(itemX, (frame.size.height - itemHeight)/2, itemWidth, itemHeight);
        
        LGBrandModel *brandModel = self.dataModel.brand[i];
        
//        btnView.brandName.text = brandModel.brand_name;
        
        [btnView setBrandCellWithModel:brandModel];
        
        btnView.centerY = brandScrollView.centerY;
        
        [brandScrollView addSubview:btnView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(brandClick:)];
        btnView.tag = i;
        [btnView addGestureRecognizer:tap];
    }
    
    return brandView;
}
- (void)brandClick:(UITapGestureRecognizer *)sender{
    LGBrandModel *brandModel = self.dataModel.brand[sender.view.tag];
    NSLog(@"%@",brandModel);
    LGHomeSearcjViewController *searchVC = [[LGHomeSearcjViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
    
}
//跳到 品牌
- (void)showNextBran:(id)recognize
{
//    UITapGestureRecognizer *tapClick = (UITapGestureRecognizer *)recognize;
//
//    UIView *membwerView = tapClick.view;
//
//    UIView *fateher = membwerView.superview;
//
//    UIScrollView *scroll = [fateher viewWithTag:421];
//
//    CGPoint scrollOffset = scroll.contentOffset;
//
////    CGFloat currentX = scrollOffset.x;
//    CGFloat nextX = scrollOffset.x + brandScrollWidth;
//
//
//    CGFloat offx = nextX >= scroll.contentSize.width - brandScrollWidth? scroll.contentSize.width - brandScrollWidth : (scrollOffset.x + brandScrollWidth);
//
//    scrollOffset.x = offx;
//
//    [UIView animateWithDuration:0.2 animations:^{
//        scroll.contentOffset = scrollOffset;
//    }];
    LGClassifyViewController *catagoryVC = [LGClassifyViewController new];
    [self.navigationController pushViewController:catagoryVC animated:YES];
}

#pragma mark -- 7种种类的界面
- (UIView *)makeStyleViewWithFram:(CGRect)frame
{
    NSInteger styleViewCount = self.dataModel.palace.count > 1 ? 7 : 4;
    
    LGPalaceModel *styleOneModel = self.dataModel.palace[0];
    
    
    LGPalaceModel *styleTwoModel = [[LGPalaceModel alloc] init];
    
    if(self.dataModel.palace.count > 1)
    {
        styleTwoModel = self.dataModel.palace[1];
    }
    
    
    UIView *styleView = [[UIView alloc] initWithFrame:frame];
    styleView.backgroundColor = RGBCOLOR(239, 241, 241);
    CGFloat spac = 1;
    CGFloat width = (SCREEN_WIDTH - 1)/2;
    CGFloat height = 100;
    
    for (int i = 0 ; i < styleViewCount; i ++) {
        LGStyleView *itemView = [[NSBundle mainBundle] loadNibNamed:@"LGStyleView" owner:self options:nil][0];
        CGFloat itemX = i%2 == 0 ? 0 : width + spac;
        CGFloat itemY = i/2 == 0 ? 0 : i/2 * (height + spac);
        if(i == 6)
        {
            itemX = width + spac;
        }
        
        CGFloat itemH = height;
        if (i == 4) {
            itemH = height * 2 + spac;
            [itemView removeConstraints:itemView.constraints];
            [itemView.styleLabel removeConstraints:itemView.styleLabel.constraints];
            [itemView.detailLabel removeConstraints:itemView.detailLabel.constraints];
            [itemView.ImageView removeConstraints:itemView.ImageView.constraints];
            [itemView.styleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(21);
                make.top.mas_equalTo(22);
                make.right.mas_equalTo(-5);
                make.height.mas_greaterThanOrEqualTo(0);
            }];
            [itemView.detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(21);
                make.top.mas_equalTo(itemView.styleLabel.mas_bottom).mas_offset(5);
                make.right.mas_equalTo(-5);
                make.height.mas_greaterThanOrEqualTo(0);
            }];
            [itemView.ImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(0);
                make.height.mas_equalTo(106);
                make.width.mas_equalTo(148);
                make.top.mas_equalTo(itemView.detailLabel.mas_bottom).mas_offset(25);
            }];
        }
        
        itemView.frame = CGRectMake(itemX, itemY, width, itemH);
        
        if(i < 4)
        {
            NSArray *itemArray = styleOneModel.item;
            
            [itemView setstyleViewWithItemModel:itemArray[i]];
        }else
        {
            NSArray *itemArray = styleTwoModel.item;
            
            [itemView setstyleViewWithItemModel:itemArray[i - 4]];
        }
        
        
        [styleView addSubview:itemView];
        
        itemView.tag = 1000 + i;
        UITapGestureRecognizer *styleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(styleClick:)];
        
        [itemView addGestureRecognizer:styleTap];
    }
    
    return styleView;
}

- (void)styleClick:(id)recognize

{
    UITapGestureRecognizer *tapClick = (UITapGestureRecognizer *)recognize;
    
    UIView *membwerView = tapClick.view;
    
    NSInteger tag = membwerView.tag - 1000;
    
    NSLog(@"click At Tag == %ld",(long)tag);
    //暂时写一个跳转热搜的
    LGBestSellerViewController *bestSellVC = [[LGBestSellerViewController alloc] init];
    [self.navigationController pushViewController:bestSellVC animated:YES];

}

#pragma mark -- 猜你喜欢

- (UIView *)makeLabelViewWithFrame:(CGRect)frame
{
    UIView *LabelView = [[UIView alloc] initWithFrame:frame];
    LabelView.backgroundColor = WHITE_COLOR;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, LabelView.width - 30, 20)];
    titleLabel.text = @"- 猜你喜欢 -";
    titleLabel.font = [UIFont boldSystemFontOfSize:15];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [LabelView addSubview:titleLabel];
    return LabelView;
}

- (void )makeRecommendProductViewWithFrame:(CGRect)frame
{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置滑动方向
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 5;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 15, SCREEN_WIDTH, SCREEN_HEIGHT - 35) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = RGBCOLOR(239, 241, 241);
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"LGDetailCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"LGDetailCollectionViewCell"];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionHeader"];
    if (@available(iOS 11.0, *)) {
        self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
//    return productView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.productDataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LGDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LGDetailCollectionViewCell" forIndexPath:indexPath];
    
    [cell setGoodCellWithGoodModle:self.productDataArray[indexPath.row]];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(floor((SCREEN_WIDTH - 5) / 2.0), 237);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LGProductDetailViewController *detailVC  = [[LGProductDetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
    NSLog(@"点击了精品推荐");
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(nonnull NSString *)kind atIndexPath:(nonnull NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView * header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"collectionHeader" forIndexPath:indexPath];
        
        //头视图添加view
        CGRect frame = self.backScrollView.frame;
        frame.size.height = self.headerHeight;
        self.backScrollView.frame = frame;
        [header addSubview:self.backScrollView];
    
        return header;
    }
    
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, self.headerHeight);
}


#pragma  mark -- 两行图片的cell
- (UIView *)makeTwoImageViewWithFrame:(CGRect )frame
{
    UIView *TwoImageView = [[UIView alloc] initWithFrame:frame];
    TwoImageView.backgroundColor = WHITE_COLOR;
    
    CGFloat imageHeight = 200;
    
    for (int i = 0; i < self.dataModel.activity.count; i ++) {
        UIImageView *imageLabelView = [[UIImageView alloc] initWithFrame:CGRectMake(0, i * (imageHeight + 10), SCREEN_WIDTH, imageHeight)];
        
        LGActivityModel *activityModel = self.dataModel.activity[i];
        [imageLabelView yy_setImageWithURL:[NSURL URLWithString:activityModel.activity_img] options:YYWebImageOptionUseNSURLCache];
        [TwoImageView addSubview:imageLabelView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(imageLabelView.frame) - 20, SCREEN_WIDTH - 20 * 2, 20)];
        label.textColor = WHITE_COLOR;
        label.text = activityModel.activity_name;
        label.font = [UIFont boldSystemFontOfSize:18];
        [imageLabelView addSubview:label];
        imageLabelView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(activityClick:)];
        imageLabelView.tag = i;
        [imageLabelView addGestureRecognizer:tap];
    }
    
    return TwoImageView;
}

- (void)activityClick:(UITapGestureRecognizer *)gesture{
    NSInteger tag = gesture.view.tag;
    LGActivityModel *activityModel = self.dataModel.activity[tag];
    NSLog(@"%@",activityModel);
    LGBestSellerViewController *bestSellVC = [[LGBestSellerViewController alloc] init];
    [self.navigationController pushViewController:bestSellVC animated:YES];
}


@end
